﻿using Modules.System.Attachment;
using PmSoft.Quartz;
using Quartz;

namespace ManagementApi.Jobs
{
    /// <summary>
    /// 系统临时附件删除任务
    /// </summary>
    [DisallowConcurrentExecution]//不可并行执行
    [JobDescription("0 0 00 * * ?")]//必须每天0点执行
    public class ClearTemporaryAttachmentJob : IJob
    {
        private readonly AttachmentService attachmentService;

        public ClearTemporaryAttachmentJob(AttachmentService attachmentService)
        {
            this.attachmentService = attachmentService;
        }

        public Task Execute(IJobExecutionContext context)
        {
            //将十分钟以前的临时附件删除
            IEnumerable<AttachmentInfo> attachmentInfos = attachmentService.GetAttachmentInfosByExpiredTemporary();
            foreach (var attachmentInfo in attachmentInfos)
            {
                attachmentService.DeleteTemporary(attachmentInfo);
            }
            return Task.CompletedTask;
        }
    }
}
